package bishiqiangxun;

public class Test2 {
    public static void solve1 (int i,int j,char[][] grid) {

        //判断一下边界情况
        if(i < 0 || i >= grid.length ||j < 0 || j >= grid[0].length || grid[i][j] == '0') {
            //返回
            return;
        }
        //把岛屿变没
        grid[i][j] = '0';  //变成海洋
        //接下来上下左右找
        solve1(i-1,j,grid);
        solve1(i+1,j,grid);
        solve1(i,j-1,grid);
        solve1(i,j+1,grid);
    }
    public int solve (char[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        int count = 0;  //记录岛屿的数量
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(grid[i][j] == '1') {//满足就相当于 创建一个岛屿
                    solve1(i,j,grid);
                    count++;
                }
            }
        }
        return count;
    }
}
